//
//  CarInsuranceCalculatorViewController.m
//  CarUser
//
//  Created by ayi on 2019/4/1.
//  Copyright © 2019 王森林. All rights reserved.
//

#import "CarInsuranceCalculatorViewController.h"

#import "CarCalculatorViewModel.h"
@interface CarInsuranceCalculatorViewController ()

@property (nonatomic, strong) CarCalculatorViewModel *viewmodel;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UILabel *shangyebaoxianLab;
@property (weak, nonatomic) IBOutlet UITextField *priceTextf;
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;

@property (weak, nonatomic) IBOutlet UILabel *thirdPartyLiabilityInsuranceLab;//第三方责任险
@property (weak, nonatomic) IBOutlet UIView *thirdPartyLiabilityInsuranceContentView;//
@property (weak, nonatomic) IBOutlet UILabel *vehicleLossInsuranceLab;//车辆损失险
@property (weak, nonatomic) IBOutlet UILabel *fullVehicleTheftandrescueLab;//全车盗抢险
@property (weak, nonatomic) IBOutlet UILabel *glassBrokenInsuranceLab;//玻璃单独破碎险
@property (weak, nonatomic) IBOutlet UIView *glassBrkenInsuranceView;//
@property (weak, nonatomic) IBOutlet UILabel *autoignitionInsuranceLab;//自燃损失
@property (weak, nonatomic) IBOutlet UILabel *nofranchiseInsuranceLab;//不计免赔特约险
@property (weak, nonatomic) IBOutlet UILabel *whithoutResponsibilityLab;//无过责任险
@property (weak, nonatomic) IBOutlet UILabel *peopleResponsibilityLab;//车上人员责任险
@property (weak, nonatomic) IBOutlet UILabel *bodyScratchesLab;//车身划痕险
@property (weak, nonatomic) IBOutlet UIView *bodyScratchesContentView;

@property (strong, nonatomic) UIScrollView *emptyScrollview;


@end

@implementation CarInsuranceCalculatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"保险计算器";
    [[[IQKeyboardManager sharedManager] disabledDistanceHandlingClasses] addObject:[self class]];
    
    self.scrollview.backgroundColor = RGB(234, 234, 234);
    self.view1.hidden = YES;
    self.view2.hidden = YES;
    
    _viewmodel = [[CarCalculatorViewModel alloc]init];
    _viewmodel.childvc = self;
    
    _emptyScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT)];
    _viewmodel.childScroll = self.emptyScrollview;
    [self.view addSubview:_emptyScrollview];
    _emptyScrollview.hidden = NO;
    
    [self addRac];
    [self.viewmodel.raccommand execute:@(1)];
}

- (void)addRac{
    RAC(self, thirdPartyLiabilityInsuranceLab.text) = RACObserve(self.viewmodel, vaule_more_1);
    RAC(self, vehicleLossInsuranceLab.text) = RACObserve(self.viewmodel, vaule_more_2);
    RAC(self, fullVehicleTheftandrescueLab.text) = RACObserve(self.viewmodel, vaule_more_3);
    RAC(self, glassBrokenInsuranceLab.text) = RACObserve(self.viewmodel, vaule_more_4);
    RAC(self, autoignitionInsuranceLab.text) = RACObserve(self.viewmodel, vaule_more_5);
    RAC(self, nofranchiseInsuranceLab.text) = RACObserve(self.viewmodel, vaule_more_6);
    RAC(self, whithoutResponsibilityLab.text) = RACObserve(self.viewmodel, vaule_more_7);
    RAC(self, peopleResponsibilityLab.text) = RACObserve(self.viewmodel, vaule_more_8);
    RAC(self, bodyScratchesLab.text) = RACObserve(self.viewmodel, vaule_more_9);
    RAC(self, shangyebaoxianLab.text) = RACObserve(self.viewmodel, vaule_more_all);

    @weakify(self);
    [[self.priceTextf.rac_textSignal throttle:0.3]subscribeNext:^(NSString * _Nullable x) {
        @strongify(self);
        self.viewmodel.carMoney = [x doubleValue];
    }];
    
    
    
    [[self.viewmodel.raccommand.executionSignals switchToLatest] subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.view1.hidden = NO;
        self.view2.hidden = NO;
        self.emptyScrollview.hidden = YES;

        
        [self.thirdPartyLiabilityInsuranceContentView removeAllSubviews];
        CGFloat width3 = BOUNDWIDTH / (self.viewmodel.PZLXDM_7.count + 2);
        [self.viewmodel.PZLXDM_7 enumerateObjectsUsingBlock:^(PZLXDMModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *onebtn = [UIButton buttonWithType:UIButtonTypeCustom];
            onebtn.tag = idx + 100;
            onebtn.titleLabel.font = [UIFont systemFontOfSize:13];
            [onebtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            [onebtn setTitle:obj.PZMC01 forState:UIControlStateNormal];
            [onebtn setImage:IMG(@"dian") forState:UIControlStateNormal];
            [onebtn setImage:IMG(@"dianheight") forState:UIControlStateSelected];
            [onebtn addTarget:self action:@selector(thirdPartyLiabilityInsuranceContentViewAction:) forControlEvents:UIControlEventTouchUpInside];
            onebtn.selected = obj.hasChoose;
            onebtn.frame = CGRectMake( idx * (width3+10), 0, width3, 44);
            [self.thirdPartyLiabilityInsuranceContentView addSubview:onebtn];
        }];
        
        [self.glassBrkenInsuranceView removeAllSubviews];
        CGFloat width4 = BOUNDWIDTH / (self.viewmodel.custom_glass_PZLXDM.count + 2);
        [self.viewmodel.custom_glass_PZLXDM enumerateObjectsUsingBlock:^(PZLXDMModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *onebtn = [UIButton buttonWithType:UIButtonTypeCustom];
            onebtn.tag = idx + 100;
            onebtn.titleLabel.font = [UIFont systemFontOfSize:13];
            [onebtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            [onebtn setTitle:obj.PZMC01 forState:UIControlStateNormal];
            [onebtn setImage:IMG(@"dian") forState:UIControlStateNormal];
            [onebtn setImage:IMG(@"dianheight") forState:UIControlStateSelected];
            [onebtn addTarget:self action:@selector(glassBrkenInsuranceViewAction:) forControlEvents:UIControlEventTouchUpInside];
            onebtn.selected = obj.hasChoose;
            onebtn.frame = CGRectMake( idx * (width4+10), 0, width4, 44);
            [self.glassBrkenInsuranceView addSubview:onebtn];
        }];
        
        [self.bodyScratchesContentView removeAllSubviews];
        CGFloat width5 = BOUNDWIDTH / (self.viewmodel.PZLXDM_10.count + 2);
        [self.viewmodel.PZLXDM_10 enumerateObjectsUsingBlock:^(PZLXDMModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *onebtn = [UIButton buttonWithType:UIButtonTypeCustom];
            onebtn.tag = idx + 100;
            onebtn.titleLabel.font = [UIFont systemFontOfSize:13];
            [onebtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            [onebtn setTitle:obj.PZMC01 forState:UIControlStateNormal];
            [onebtn setImage:IMG(@"dian") forState:UIControlStateNormal];
            [onebtn setImage:IMG(@"dianheight") forState:UIControlStateSelected];
            [onebtn addTarget:self action:@selector(bodyScratchesContentViewAction:) forControlEvents:UIControlEventTouchUpInside];
            onebtn.selected = obj.hasChoose;
            onebtn.frame = CGRectMake( idx * (width5+10), 0, width5, 44);
            [self.bodyScratchesContentView addSubview:onebtn];
        }];
        
        if (!self.priceTextf.isFirstResponder) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.priceTextf becomeFirstResponder];
            });
        }
        
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}


- (void)thirdPartyLiabilityInsuranceContentViewAction:(UIButton *)btn{
    @weakify(self);
    [self.viewmodel.PZLXDM_7 enumerateObjectsUsingBlock:^(PZLXDMModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        obj.hasChoose = NO;
        if (idx == btn.tag - 100) {
            obj.hasChoose = YES;
            [self.viewmodel moreDataInitMore];
        }
        UIButton *onebtn = [self.thirdPartyLiabilityInsuranceContentView viewWithTag:idx + 100];
        onebtn.selected = obj.hasChoose;
    }];
}

- (void)glassBrkenInsuranceViewAction:(UIButton *)btn{
    @weakify(self);
    [self.viewmodel.custom_glass_PZLXDM enumerateObjectsUsingBlock:^(PZLXDMModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        obj.hasChoose = NO;
        if (idx == btn.tag - 100) {
            obj.hasChoose = YES;
            [self.viewmodel moreDataInitMore];
        }
        UIButton *onebtn = [self.glassBrkenInsuranceView viewWithTag:idx + 100];
        onebtn.selected = obj.hasChoose;
    }];
}

- (void)bodyScratchesContentViewAction:(UIButton *)btn{
    @weakify(self);
    [self.viewmodel.PZLXDM_10 enumerateObjectsUsingBlock:^(PZLXDMModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        obj.hasChoose = NO;
        if (idx == btn.tag - 100) {
            obj.hasChoose = YES;
            [self.viewmodel moreDataInitMore];
        }
        UIButton *onebtn = [self.bodyScratchesContentView viewWithTag:idx + 100];
        onebtn.selected = obj.hasChoose;
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
